<?php

namespace App\Console\Commands;


use App\Helpers\Curl;
use App\Helpers\Logger\SLogger;
use App\Models\Orm\CoinMarket;
use App\Models\Orm\CoinTicker;
use Illuminate\Console\Command;
use DB;

class GetTiker extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'GetTiker';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '主动发起查询事件';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
//        $urlConfig = "https://api.coinmarketcap.com/v1/ticker/?convert="."&limit=1535";


        $urlConfig = "https://api.coinmarketcap.com/v1/ticker/?convert=CNY&limit=2";

            $datas = Curl::curl($urlConfig);

            $datas = json_decode($datas,true);

            if(!empty($datas))
            {
                foreach ($datas as $data) {

                    //转化大小写
                    $market = strtolower("cny");

//                    DB::beginTransaction();
                    try {
                        $data['nid'] = $data['id'];
                        $data['price'] = $data['price_'.$market];
                        $data['24h_volume'] = $data['24h_volume_'.$market];
                        $data['market_cap'] = $data['market_cap_'.$market];
                        unset($data['id']);
                        unset($data['price_'.$market]);
                        unset($data['24h_volume_'.$market]);
                        unset($data['market_cap_'.$market]);
                        CoinMarket::updateOrCreate(['nid' => $data['nid']],$data);

//                        DB::commit();
                    } catch (\Exception $e) {

                        SLogger::getStream()->info("数据导入失败");
                        SLogger::getStream()->info($e);
//                        DB::rollBack();
                    }



                }
            }





    }
}
